package com.tuyrt.`as`.proj.kotlin_demo

import android.app.Activity
import android.view.View
import androidx.annotation.IdRes

/**
 * author: Administrator on 2021/5/20.
 * desc:
 */
fun main() {
//    val list = mutableListOf(1, 2, 3, 4)
//    list.swap(0, 3)
//    println("list.swap(0, 3): $list")
//    println("1241A".lastChar)
    Jump.print("aaa")
}

fun MutableList<Int>.swap(index1: Int, index2: Int) {
    val temp = this[index1]
    this[index1] = this[index2]
    this[index2] = temp
}

fun <T> MutableList<T>.swap2(index1: Int, index2: Int) {
    val temp = this[index1]
    this[index1] = this[index2]
    this[index2] = temp
}

/**--------------------扩展属性-----------------------*/
//为string添加一个lastChar的属性，用于获取字符串的最后一个字符
val String.lastChar: Char get() = this.get(this.length - 1)

class Jump {
    companion object {}
}

fun Jump.Companion.print(str: String) {
    println("Jump Companion print:$str")
}

data class Room(val address: String)

fun testRun(room: Room) {
    // 直接使用调用对象的属性
    room.run {
        println(address)
    }
}

fun testApply() {
    // 用于实例初始化
    ArrayList<String>().apply {

    }
}

fun <T : View> Activity.find(@IdRes id: Int): T {
    return findViewById(id)
}

fun Int.onClick(activity: Activity, click: () -> Unit) {
    activity.find<View>(this).apply {
        setOnClickListener { click() }
    }
}
